import requests
import pandas as pd


def get_content(url, data):
    name_list = []
    spcode_list = []
    year_list = []
    degree_list = []
    boy_girl_list = []
    fivesalaryavg_list = []
    resp = requests.post(url, headers=headers, data=data).json()
    for each in resp['data']['item']:
        name = each['name']
        spcode = each['spcode']
        limit_year = each['limit_year']
        degree = each['degree']
        boy_rate = each['boy_rate']
        girl_rate = each['girl_rate']
        boy_girl = boy_rate + ':' + girl_rate
        fivesalaryavg = each['fivesalaryavg']
        print(name)
        name_list.append(name)
        spcode_list.append(spcode)
        year_list.append(limit_year)
        degree_list.append(degree)
        boy_girl_list.append(boy_girl)
        fivesalaryavg_list.append(fivesalaryavg)

    data_dic['专业名'] = name_list
    data_dic['专业代码'] = spcode_list
    data_dic['修业年限'] = year_list
    data_dic['授予学位'] = degree_list
    data_dic['男女比例'] = boy_girl_list
    data_dic['平均薪酬'] = fivesalaryavg_list


if __name__ == '__main__':
    data_dic = {}
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    url_list = ['https://api.zjzw.cn/web/api/?keyword=&level1=1&level2=&level3=&page=1&size=30&sort=&uri=apidata/api/gkv3/special/lists&signsafe=845895dae545d9d5c9101b71bcfd4b0c', 'https://api.zjzw.cn/web/api/?keyword=&level1=1&level2=&level3=&page=1&size=30&sort=view_total&uri=apidata/api/gkv3/special/lists&signsafe=86bd3c0082e781205dbbc4899f2f0b0b', 'https://api.zjzw.cn/web/api/?keyword=&level1=1&level2=&level3=&page=1&size=30&sort=fivesalaryavg&sorttype=desc&uri=apidata/api/gkv3/special/lists&signsafe=a672c5ffbc461c9806deed9af0bf5344']
    data_list = ['{"keyword":"","level1":"1","level2":"","level3":"","page":1,"signsafe":"845895dae545d9d5c9101b71bcfd4b0c","size":30,"sort":"","uri":"apidata/api/gkv3/special/lists"}', '{"keyword":"","level1":"1","level2":"","level3":"","page":1,"signsafe":"86bd3c0082e781205dbbc4899f2f0b0b","size":30,"sort":"view_total","uri":"apidata/api/gkv3/special/lists"}', '{"keyword":"","level1":"1","level2":"","level3":"","page":1,"signsafe":"a672c5ffbc461c9806deed9af0bf5344","size":30,"sort":"fivesalaryavg","sorttype":"desc","uri":"apidata/api/gkv3/special/lists"}']
    for url, data in zip(url_list, data_list):
        get_content(url, data)
        df = pd.DataFrame(data_dic)
        if '845895dae545d9d5c9101b71bcfd4b0c' in url:
            path_name = '默认排序'
        elif '86bd3c0082e781205dbbc4899f2f0b0b' in url:
            path_name = '人气排序'
        else:
            path_name = '薪酬排序'
        df.to_excel(f'{path_name}.xlsx', index=False)
    print('存储完毕！')
